package arina.university.controller;

import arina.university.dao.StudentDAO;
import arina.university.dao.factory.DAOFactory;
import arina.university.model.Student;
import arina.university.view.StudentView;

import java.util.Collection;

/**
 * Created with IntelliJ IDEA.
 * User: user
 * Date: 26.11.13
 * Time: 16:12
 * To change this template use File | Settings | File Templates.
 */
public class StudentController {

    private StudentView view;

    public StudentController(StudentView view) {

        this.view = view;
    }

    DAOFactory memoryFactory = DAOFactory.getDAOFactory(DAOFactory.MEMORY);
    StudentDAO dao = memoryFactory.getStudentDAO();


    public Student createStudent(String name, int id, String enterDate) {

        try {
            Student student = dao.create(name, id, enterDate);
            return student;
        } catch (Exception e) {
            System.out.println(e);
            return null;
        }

    }


    public void removeStudent(int id) {

        try {
            dao.delete(id);
        } catch (Exception e) {
            System.out.println(e);
        }
    }

    public Student getStudent(int id) {
        try {
            return dao.find(id);
        } catch (Exception e) {
            System.out.println(e);
            return null;
        }

    }

    public Collection<Student> getAllStudents() {
        try {
            return dao.findAll();
        } catch (Exception e) {
            System.out.println(e);
            return null;
        }
    }

    public void findStudent(int id) {
        try {
            Student student = dao.find(id);

            view.getStudentInfo(student);
        } catch (Exception e) {
            System.out.println(e);
        }


    }

    public void updateStudent(int id, String name, int newId, String enterDate) {
        try {
            Student student = dao.find(id);

            dao.delete(student.getId());
            student.setName(name);
            student.setId(newId);
            student.setEnterDate(enterDate);
            dao.save(student);
        } catch (Exception e) {
            System.out.println(e);
        }

    }

    public void findAllStudents() {
        try {
            Collection<Student> students = dao.findAll();
            view.getStudentsInfo(students);
        } catch (Exception e) {
            System.out.println(e);
        }

    }
}
